package com.example.service.impl;

import ch.qos.logback.classic.Logger;
import com.example.entity.ElevatorAppraisal;
import com.example.entity.ElevatorArchives;
import com.example.ext.entity.ExtElevatorAppraisal;
import com.example.mapp.ElevatorArchivesMapp;
import com.example.service.ElevatorArchivesService;
import com.example.util.ResultUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by youkun on 2017/10/20.
 */
@Service
public class ElevatorArchivesServiceImpl implements ElevatorArchivesService {

    private final static Logger logger = (Logger) LoggerFactory.getLogger(ElevatorArchivesServiceImpl.class);
    @Autowired
    private ElevatorArchivesMapp elevatorArchivesMapp;

    @Override
    public Map<String, Object> queryById(String id) {
        ElevatorArchives elevatorArchives =new ElevatorArchives();
        try {
            elevatorArchives = elevatorArchivesMapp.queryById(id);

        } catch (Exception e) {
            logger.error(e.getMessage());
            return ResultUtil.returnError("查询失败"+e.getMessage(),null);
        }
        return ResultUtil.returnSuccess("查询成功",elevatorArchives);
    }


    @Override
    @Transactional
    public Map<String, Object> add(ElevatorArchives elevatorArchives) {
        ElevatorArchives elevatorArchivesss = null;
        try{
            elevatorArchivesss = elevatorArchivesMapp.queryById(elevatorArchives.getUsedPartyId()+"");
            if(elevatorArchivesss==null){
                elevatorArchivesMapp.add(elevatorArchives);
            }else {
                elevatorArchivesMapp.deleteById(elevatorArchives.getUsedPartyId());
                elevatorArchivesMapp.add(elevatorArchives);
            }
        }catch (Exception e){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            logger.error(e.getMessage());
            return ResultUtil.returnError("查询失败"+e.getMessage(),null);
        }
        return ResultUtil.returnSuccess("查询成功",elevatorArchives);
    }

    @Override
    public Map<String, Object> update(ElevatorArchives elevatorArchives) {
        try{

            elevatorArchivesMapp.update(elevatorArchives);

        }catch (Exception e){
            logger.error(e.getMessage());
            return ResultUtil.returnError("转移失败"+e.getMessage(),null);
        }
        return ResultUtil.returnSuccess("转移成功",elevatorArchives);
    }
}
